package com.fitso.model.dao.hibernate;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.springframework.stereotype.Repository;

import com.fitso.model.bean.nutrition.Serving;
import com.fitso.model.dao.ServingDao;

@Repository("HibernateServingDao")
public class HibernateServingDao extends HibernateBaseDao implements ServingDao {
	public List<Serving> read() {
		return toList(getTemplate().loadAll(Serving.class), Serving.class);
	}

	public Serving readById(Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Serving.class);
		criteria.add(Property.forName("id").eq(id));

		return firstItem(getTemplate().findByCriteria(criteria), Serving.class);
	}

	public void createOrUpdate(Serving serving) {
		getTemplate().saveOrUpdate(serving);
	}

	public void saveOrUpdate(List<Serving> servings) {
		getTemplate().saveOrUpdateAll(servings);
	}

	public void delete(Serving serving) {
		getTemplate().delete(serving);
	}

	public void delete(List<Serving> servings) {
		getTemplate().deleteAll(servings);
	}
}
